package com.SmartLED.Controller;

import com.SmartLED.Common.Error_config;
import com.SmartLED.Common.SYS_Base;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by Frozen on 2017/1/9.
 */
public class BaseController extends SYS_Base {

    protected Map params;

    /**
     * 解码
     * @param request
     */
    protected void decode(HttpServletRequest request){
        // TODO: 2017/1/9 验证需要的参数是否存在,并且参数类型是否是需要的类型
        this.params = request.getParameterMap();
        if (rsContains("operator_id")) {
            this.operator_id = Integer.parseInt(params.get("operator_id").toString());
        }
    }

    //controller中产生的错误为参数不足
    protected String sendError() throws JsonProcessingException {
        return sendErrorByCode(Error_config.E_1001);
    }

    //请求参数中有该项参数
    protected boolean rsContains(String key){
        return this.params.containsKey(key) && !this.params.get(key).equals("");
    }
}
